﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using XingLucifer.IBase;

namespace XingLucifer.Devices.Inits
{
    public abstract class DevicePing : IDeviceInit
    {
        private XingLucifer.IBase.Enums.DeviceTypes _name;
        protected Action<string> _action;
        public XingLucifer.IBase.Enums.DeviceTypes Name => _name;
        public DevicePing(XingLucifer.IBase.Enums.DeviceTypes name, Action<string> action)
        {
            _name = name;
            _action = action;
        }

        public virtual bool Init(string? sourceIP, string? ip, string? dns, int port)
        {
            return true;
        }

        public virtual bool Ping(string? ip = null)
        {
            Ping pingSender = new Ping();
            PingOptions options = new PingOptions();
            options.DontFragment = true;
            string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            byte[] buffer = Encoding.ASCII.GetBytes(data);
            PingReply? reply = null;
            for (int i = 0; i < 3; i++)
            {
                if (reply != null) { break; }
                reply = pingSender.Send(ip, 1000, buffer, options);
                System.Threading.Thread.Sleep(1000);
            }
            return reply.Status == IPStatus.Success;
        }

        public virtual bool Reboot()
        {
            return true;
        }
    }
}
